package net.rokyinfo.appcontroller.auth;

import com.sun.net.httpserver.HttpPrincipal;
import io.netty.handler.codec.http.HttpHeaders;
import net.rokyinfo.appcontroller.bean.HttpNettyRequest;

import java.util.Base64;

public abstract class BasicAuthenticator extends Authenticator {
    protected String realm;

    public BasicAuthenticator(String var1) {
        this.realm = var1;
    }

    public String getRealm() {
        return this.realm;
    }

    public Result authenticate(HttpNettyRequest var1) {
        HttpHeaders var2 = var1.getHttpRequest().headers();
        String var3 = var2.get("Authorization");
        if(var3 == null) {
            HttpHeaders var11 = var1.getHttpRequest().headers();
            var11.set("WWW-Authenticate", "Basic realm=\"" + this.realm + "\"");
            return new Retry(401);
        } else {
            int var4 = var3.indexOf(32);
            if(var4 != -1 && var3.substring(0, var4).equals("Basic")) {
                byte[] var5 = Base64.getDecoder().decode(var3.substring(var4 + 1));
                String var6 = new String(var5);
                int var7 = var6.indexOf(58);
                String var8 = var6.substring(0, var7);
                String var9 = var6.substring(var7 + 1);
                if(this.checkCredentials(var8, var9)) {
                    return new Success(user);
                } else {
                    HttpHeaders var10 = var1.getHttpRequest().headers();
                    var10.set("WWW-Authenticate", "Basic realm=\"" + this.realm + "\"");
                    return new Failure(401);
                }
            } else {
                return new Failure(401);
            }
        }
    }

    public abstract boolean checkCredentials(String var1, String var2);
}